package cn.unknowpupil.init.controller.monitor;


import cn.unknowpupil.init.common.enums.BusinessType;
import cn.unknowpupil.init.common.resopnse.ResultResponse;
import cn.unknowpupil.init.components.annotation.Log;
import cn.unknowpupil.init.domain.system.SysDictData;
import cn.unknowpupil.init.service.system.ISysDictDataService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * Created at 2020-10-30 by  w10g <br>
 * Initialize SysDictData  ... <br>
 * 字典数据表 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-字典数据表模块"}, value = "SysDictData")
@RestController
@RequestMapping(value = "admin/sys-dict-data")
public class SysDictDataController {

    @Resource
    private ISysDictDataService SysDictDataService;

    /**
     * 新增
     *
     * @param SysDictData {@link SysDictData}  字典数据表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：新增")
    @Log(title = "新增字典", businessType = BusinessType.INSERT)
    @PreAuthorize("@ps.hasPerm('baseinfo:dict:create')")
    @PostMapping("create")
    public ResultResponse create(@Valid @RequestBody SysDictData SysDictData, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
            return ResultResponse.buildFailure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean created = SysDictDataService.create(SysDictData);
        if (created) {
            return ResultResponse.success("创建成功");
        } else {
            return ResultResponse.buildFailure("创建失败");
        }

    }

    /**
     * 修改
     *
     * @param SysDictData {@link SysDictData}  字典数据表
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：修改")
    @Log(title = "修改字典", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ps.hasPerm('baseinfo:dict:update')")
    @PutMapping("update")
    public ResultResponse update(@Valid @RequestBody SysDictData SysDictData, BindingResult bindingResult) {
        // 表单验证
        if (bindingResult.hasErrors()) {
            return ResultResponse.buildFailure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }

        // 业务逻辑
        boolean update = SysDictDataService.update(SysDictData);
        if (update) {
            return ResultResponse.success("修改成功");
        } else {
            return ResultResponse.buildFailure("修改失败");
        }

    }

    /**
     * 获取
     *
     * @param id {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("get/{id}")
    public ResultResponse get(@PathVariable("id") Long id) {
        SysDictData SysDictData = SysDictDataService.info(id);
        return ResultResponse.buildSuccess(SysDictData);
    }

    /**
     * 获取
     *
     * @param dictType {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：获取")
    @GetMapping("type/get/{dictType}")
    public ResultResponse get(@PathVariable("dictType") String dictType) {
        List<SysDictData> sysDictDataList = SysDictDataService.selectDictDataByType(dictType);
        return ResultResponse.buildSuccess(sysDictDataList);
    }

    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：删除")
    @Log(title = "删除字典", businessType = BusinessType.DELETE)
    @PreAuthorize("@ps.hasPerm('baseinfo:dict:delete')")
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted = SysDictDataService.delete(ids);
        if (deleted) {
            return ResultResponse.success("删除成功");
        } else {
            return ResultResponse.buildFailure("删除失败");
        }
    }

    /**
     * 分页
     *
     * @param current {@code int} 页码
     * @param size    {@code int} 笔数
     * @return {@link ResultResponse}
     */
    @ApiOperation("系统：分页")
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysDictData>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysDictData sysDictData) {
        IPage<SysDictData> page = SysDictDataService.page(current, size, sysDictData);
        return ResultResponse.buildSuccess(page);
    }



}
